package 尚硅谷.juc.test9;

import java.util.concurrent.locks.ReentrantLock;

public class Test9 {
    private static ReentrantLock reentrantLock=new ReentrantLock();
    /**
     * 一个main线程，在执行中获取三次同样的reentrantLock锁，而不产生死锁，这就叫做重入锁
     *
     */
    public static void main(String[] args) {
        //main线程先在主方法中获取reentrantLock锁
        reentrantLock.lock();
        try {
            System.out.println("main........................");
            //接着main线程又调用t1()在t1方法中获取reentrantLock锁
            t1();
        }finally {
            reentrantLock.unlock();
        }
    }

    public static void t1(){
        reentrantLock.lock();
        try {
            System.out.println("t1........................");
            t2();
        }finally {
            reentrantLock.unlock();
        }
    }

    public static void t2(){
        //接着main线程又在t2()中获得reentrantLock锁
        reentrantLock.lock();
        try {
            System.out.println("t2........................");
        }finally {
            reentrantLock.unlock();
        }
    }
}
